package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.CollectionBehaviorDto;
import com.heima.behavior.pojos.ApCollectionBehavior;
import com.heima.behavior.pojos.ApLikesBehavior;
import com.heima.behavior.service.ApCollectionBehaviorService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.ApThreadLocalUtil;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author Gareth Cao
 * @date 2022/6/25 19:53
 */
@Service
public class ApCollectionBehaviorServiceImpl implements ApCollectionBehaviorService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public ResponseResult collection(CollectionBehaviorDto dto) {
        //1 校验参数
        if (dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2 获取用户登录id
        ApUser apUser = ApThreadLocalUtil.getUser();
        if (apUser == null) {
            //未登录
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        if (apUser.getId() == 0) {
            //游客登录
            return ResponseResult.errorResult(AppHttpCodeEnum.VISITOR_NEED_LOGIN);
        }
        //3 查询用户是否已经收藏
        //3.1 定义收藏行为的key,hash存储数据，大key为文章id，小key为用户id
        String collection_key = "USER_COLLECTION_" + dto.getArticleId();
        //3.2 从redis当中查出是否包含key
        Boolean flag = stringRedisTemplate.opsForHash().hasKey(collection_key, apUser.getId() + "");
        //4 如果已经收藏，并正在执行收藏操作，那么直接返回
        if (flag && dto.getOperation() == 0) {
            return ResponseResult.okResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //5 如果已经收藏了，并执行取消操作，则删除redis和mongo
        if (flag && dto.getOperation() == 1) {
            //删除redis
            stringRedisTemplate.opsForHash().delete(collection_key, apUser.getId() + "");
            //删除mongo
            Query query = Query.query(Criteria.where("userId").is(apUser.getId())
                    .and("articleId").is(dto.getArticleId()));
            mongoTemplate.remove(query, ApCollectionBehavior.class);
            return ResponseResult.okResult(AppHttpCodeEnum.COLLECTION_CANCEL);
        }
        //6 未收藏并执行收藏操作，则添加redis和mongo
        if (!flag && dto.getOperation() == 0) {
            //添加redis
            stringRedisTemplate.opsForHash().put(collection_key, apUser.getId() + "", "1");
            //添加mongo
            ApCollectionBehavior apCollectionBehavior = new ApCollectionBehavior();
            apCollectionBehavior.setId(ObjectId.get().toHexString());
            apCollectionBehavior.setUserId(apUser.getId().longValue());
            apCollectionBehavior.setArticleId(dto.getArticleId());
            apCollectionBehavior.setType(dto.getType());
            apCollectionBehavior.setCollectionTime(dto.getCreateTime());
            mongoTemplate.insert(apCollectionBehavior);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return null;
    }
}
